package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public SortedListOf<ContactData> getUIContacts() {
        manager.navigateTo().mainPage();
        SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
        List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
       
        for (int i=0;i<rows.size();i++) {
                ContactData contact = new ContactData()
                        .withfirstName(rows.get(i).findElement(By.xpath("./td[3]")).getText())
                        .withLastName(rows.get(i).findElement(By.xpath("./td[2]")).getText());
                        contacts.add(contact);
        }      
       
        return contacts;
    }


	public ContactHelper CreateContact(ContactData contact, boolean formType) {
		manager.navigateTo().ContactCreatePage();
		fillContactForm(contact, CREATION);
		initContactCreation();
		returnToHomePage();
		return this;
	}

	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		selectContactByType(index);
		selectByEdit();
		submitContactDeletion();
		returnToHomePage();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact,
			boolean MODIFICATION) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		ContactData name = new ContactData().withLastName("new last name");
		fillContactForm(name, ContactHelper.MODIFICATION);
		submitContactModification();
		returnToHomePage();
		return this;
	}

	// --------------------------------------------------------

	public ContactHelper initContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomephone());
		type(By.name("mobile"), contact.getMobile());
		type(By.name("work"), contact.getWorkphone());
		type(By.name("email"), contact.getEmail1());
		type(By.name("email2"), contact.getEmail1());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		if (formType == CREATION) {
			// selectByText(By.name("new_group"), contact.group);
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error(
						"Group selector exists in contact modification form");
			}
		}
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	private void selectByEdit() {
		click(By.cssSelector("img[alt=\"Edit\"]"));
	}

	private void selectContactByType(int index) {
		click(By.xpath("//tbody/tr[" + (index + 1) + "]/td/input"));
	}

	public ContactHelper initContactModification(int index) {
		selectContactByType(index);
		selectByEdit();
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("(//input[@type='submit'])[@value='Update']"));
		return this;
	}

	private ContactHelper submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
		return this;
	}
}
